//
//  StockMoreDataTableViewController.m
//  RedManStock
//
//  Created by lanou3g on 15/7/29.
//  Copyright (c) 2015年 lanou3g. All rights reserved.
//

#import "StockMoreDataTableViewController.h"
#import "Stock.h"
#import "MyHandle.h"
#import "ParameterHeader.h"
#import "UIDefine.h"
@interface StockMoreDataTableViewController ()

@property (nonatomic,retain) NSMutableDictionary *myDic;

@end

@implementation StockMoreDataTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
    
    //http://api.gongniucaijing.com/Stock/quota?code=600369&idfa=7B7C9B42-5C18-4EEB-A0F8-6708DE081AAE&machine=iPhone%205&os=iPhone%20OS%208.2&platform=1&protocolVersion=1.0&qudao=0000001&resolution=640%2A1136&token=d58f02522dac7e698a4600c8b18ff02c0b9fa6df&uuid=BULLSTOCK-79460017-C3E5-45EC-B98C-1DD541A61127&version=1.9.0&versionCode=190
    
    self.myDic=[NSMutableDictionary dictionary];
    [self loadNewData];
    
    //设置导航栏
    self.navigationItem.titleView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, 150, 30)];
    UILabel *label1=[UILabel labelWithFrame:CGRectMake(0, 0, 150, 20) text:[NSString stringWithFormat:@"%@(%@)",self.stock.name,self.stock.code]];
    label1.font=[UIFont systemFontOfSize:14];
    label1.textAlignment=NSTextAlignmentCenter;
    UILabel *label2=[UILabel labelWithFrame:CGRectMake(0, 20, 150, 10) text:self.stock.statusTime];
    label2.font=[UIFont systemFontOfSize:11];
    label2.textAlignment=NSTextAlignmentCenter;
    label1.textColor=[UIColor whiteColor];
    label2.textColor=[UIColor whiteColor];
    [self.navigationItem.titleView addSubview:label1];
    [self.navigationItem.titleView addSubview:label2];
    
    //去掉下方多余的线
    UIView *view=[[UIView alloc]initWithFrame:CGRectZero];
    self.tableView.tableFooterView=view;
}

- (void)loadNewData{
    NSString *url=@"http://api.gongniucaijing.com/Stock/quota";
    [[MyHandle new] requestDataWithURLStr:url parameterBlock:^NSDictionary *{
        NSDictionary *dic=@{@"code":self.stock.code,
                            @"token":Token};
        return dic;
    } successBlock:^(NSDictionary *responseDic) {
        self.myDic=[responseDic objectForKey:@"data"];
        [self.tableView reloadData];
    } errorBlock:^(NSError *error) {
        NSLog(@"error=%@",error);
    }];
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
#warning Potentially incomplete method implementation.
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
#warning Incomplete method implementation.
    // Return the number of rows in the section.
    return 9;
}

//    JSON
//    {
//        "status": 0,
//
//        "data": {
//            "priceEarningRatio": 13.301470588235,    //市盈率 13.01
//            "priceNetRatio": 2.8944,    // 市净率  2.83
//            "turnoverRate": 3.5778151505919,     // 换手率   3.57%
//            "amplitudeRate": 0.11375387797311,   //  振幅   11.87%
//            "amount": 1588870778,     //  成交额   15.89亿
//            "totalMarketValue": 51060012026.58,    //总市值   510.60亿
//            "circulMarketValue": 42015012026.58,   // 流通市值  420.15亿
//            "navps": 6.25,    // 每股净资产  6.25
//            "eps": 0.34,   //  每股收益
//            "status": 2,     // 估计是标题中的“交易中”
//            "statusTime": 1436942221        //时间   能转换成具体时间   07-15 14:44:12
//        },
//        "msg": ""
//    }

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 25;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UILabel *label=[UILabel labelWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 25) text:@"指标"];
    label.font=[UIFont systemFontOfSize:14];
    label.textAlignment=NSTextAlignmentCenter;
    label.backgroundColor=[UIColor grayColor];
    return label;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *identifier=@"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell==nil) {
        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:identifier];
    }
    if (indexPath.row==0) {
        CGFloat f0=[[_myDic objectForKey:@"priceEarningRatio"] floatValue];
        cell.textLabel.text=@"市盈率";
        cell.detailTextLabel.text=[NSString stringWithFormat:@"%.2f",f0];
    }
    else if(indexPath.row==1){
        CGFloat f0=[[_myDic objectForKey:@"priceNetRatio"] floatValue];
        cell.textLabel.text=@"市净率";
        cell.detailTextLabel.text=[NSString stringWithFormat:@"%.2f",f0];
    }
    else if(indexPath.row==2){
        CGFloat f0=[[_myDic objectForKey:@"turnoverRate"] floatValue];
        cell.textLabel.text=@"换手率";
        cell.detailTextLabel.text=[NSString stringWithFormat:@"%.2f%%",f0];
    }
    else if(indexPath.row==3){
        CGFloat f0=[[_myDic objectForKey:@"amplitudeRate"] floatValue]*100;
        cell.textLabel.text=@"振幅";
        cell.detailTextLabel.text=[NSString stringWithFormat:@"%.2f%%",f0];
    }
    else if(indexPath.row==4){
        CGFloat f0=[[_myDic objectForKey:@"amount"] floatValue]/100000000;
        cell.textLabel.text=@"成交额";
        cell.detailTextLabel.text=[NSString stringWithFormat:@"%.2f亿",f0];
    }
    else if(indexPath.row==5){
        CGFloat f0=[[_myDic objectForKey:@"totalMarketValue"] floatValue]/100000000;
        cell.textLabel.text=@"总市值";
        cell.detailTextLabel.text=[NSString stringWithFormat:@"%.2f亿",f0];
    }
    else if(indexPath.row==6){
        CGFloat f0=[[_myDic objectForKey:@"circulMarketValue"] floatValue]/100000000;
        cell.textLabel.text=@"流通市值";
        cell.detailTextLabel.text=[NSString stringWithFormat:@"%.2f亿",f0];
    }
    else if(indexPath.row==7){
        CGFloat f0=[[_myDic objectForKey:@"navps"] floatValue];
        cell.textLabel.text=@"每股净资产";
        cell.detailTextLabel.text=[NSString stringWithFormat:@"%.2f",f0];
    }
    else{
        CGFloat f0=[[_myDic objectForKey:@"eps"] floatValue];
        cell.textLabel.text=@"每股收益";
        cell.detailTextLabel.text=[NSString stringWithFormat:@"%.2f",f0];
    }
    return cell;
}


/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
